package com.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 * 1. 节省资源
 * 2. 管理线程
 */
public class ThreadPool {

    public static void main(String[] args) throws InterruptedException {

        ExecutorService es = Executors.newFixedThreadPool(100);
        long t1 = System.currentTimeMillis();
        for (int i = 1; i <= 50; i++) {
             es.execute(new Thread1()); //异步执行（同时执行)
        }
        long t2 = System.currentTimeMillis();
        es.shutdown(); //不接收新的线程加入线程池

        //等待线程池所有子线程执行完后，才执行下面代码，如果一分钟还没执行完就执行下面
        es.awaitTermination(1, TimeUnit.MINUTES);

        System.out.println(t2-t1);

    }

}
